package com.tianci.appstore.ui.search;

import android.content.Context;
import android.graphics.Color;
import android.text.Html;
import android.view.Gravity;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.coocaa.x.framework.app.CoocaaApplication;
import com.coocaa.x.framework.utils.LogUtils;
import com.coocaa.x.uipackage.widget.SlideFocusView;
import com.tianci.appstore.R;
import com.tianci.appstore.audio.IAudio;
import com.tianci.appstore.controller.AppStoreSearchController;
import com.tianci.appstore.ui.SkinManager;
import com.tianci.appstore.ui.control.SkyMainPageController.OnPageChangeListener;
import com.tianci.appstore.ui.data.SkyListAppData;
import com.tianci.appstore.ui.data.SkyListAppData.ListType;
import com.tianci.appstore.ui.list.ListBaseView;
import com.tianci.appstore.ui.list.ListFlipper;
import com.tianci.appstore.ui.list.SkyListAdapter;
import com.tianci.appstore.ui.list.SkyListAdapter.OnItemClickListener;
import com.tianci.appstore.ui.util.UICommon;
import com.tianci.appstore.ui.widget.SkyAppPager.Direction;
import com.tianci.appstore.ui.widget.SkyCustomLayout;
import com.tianci.appstore.ui.widget.SkyCustomLayout.BlockData;
import com.tianci.appstore.ui.widget.SkyCustomLayout.CustomLayoutParams;
import com.tianci.appstore.utils.SkyConstants;

import java.util.ArrayList;
import java.util.List;

public class SearchListView extends ListBaseView
        implements OnItemClickListener, OnPageChangeListener {
    protected Context mContext;

    protected static final int UPDATE_LIST = 0;
    protected static final int UPDATE_STATUS = 1;
    protected static final int UPDATE_PERCENT = 2;
    protected static final int DELAY_DISSMISS_DIALOG = 3;
    protected static final int UPDATE_SUCCESS_STATE = 4;
    protected static final int UPDATE_FAILED_STATE = 5;

    protected FrameLayout contentLayout;
    protected TextView tipText;
    private TextView mTitleView = null;
    private TextView mNoSearchTip = null;
    protected ListFlipper listFlipper;
    protected TextView pageNumberText;
    private SkyCustomLayout mHotLayout = null;

    protected SearchController mController;

    protected ListType listType;

    protected static int titleSize = SkyConstants.Dpi(42);
    protected static int rightTopTextSize = SkyConstants.Dpi(39);

    protected int recoverPageIndex = 0;     // 列表出现变动时，刷新数据之后，UI显示第几页
    private SlideFocusView.FocusChangedEvent mFocusChangedEvent = null;
    private OnShowLoadingListener mShowLoadingListener = null;
    private boolean isGame = false;

    public SearchListView(Context context, ListType listType) {
        super(context);
        this.mContext = context;
        this.listType = listType;
    }

    public interface OnShowLoadingListener {
        public void showLoading(boolean show);
    }

    public void createView() {
        //SkinManager.setBackground(this, R.drawable.bg);
        contentLayout = new FrameLayout(mContext);
        this.addView(contentLayout, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        addTitle();
        initFlipper();
        addHotLayout();
        initPageNoView();
        addTips();
        showHotLayout(true);
    }

    public void setShowLoadingListener(OnShowLoadingListener listener) {
        this.mShowLoadingListener = listener;
    }

    public void showHotLayout(boolean show) {
        if (show) {
            if (mNoSearchTip != null)
                mNoSearchTip.setVisibility(View.VISIBLE);
            mTitleView.setVisibility(View.VISIBLE);
            mHotLayout.setVisibility(View.VISIBLE);
            listFlipper.setVisibility(View.INVISIBLE);
            pageNumberText.setVisibility(View.INVISIBLE);
        } else {
            if (mNoSearchTip != null)
                mNoSearchTip.setVisibility(View.INVISIBLE);
            mTitleView.setVisibility(View.INVISIBLE);
            mHotLayout.setVisibility(View.INVISIBLE);
            listFlipper.setVisibility(View.VISIBLE);
            pageNumberText.setVisibility(View.VISIBLE);
        }
    }

    public void showSearchResultText(String key, int totalCount) {
        if(!isGame) {
            mTitleView.setVisibility(View.VISIBLE);
            mTitleView.setText(String.format("与 “%s” 相关的搜索共%d个", key, totalCount));
            LayoutParams textParams = new LayoutParams(LayoutParams.WRAP_CONTENT,
                    LayoutParams.WRAP_CONTENT, Gravity.TOP | Gravity.LEFT);
            textParams.setMargins(SkyConstants.Div(105), SkyConstants.Div(90), 0, 0);
            contentLayout.removeView(mTitleView);
            contentLayout.addView(mTitleView, textParams);
        }
    }

    public void setFocusChangeEvent(SlideFocusView.FocusChangedEvent event) {
        this.mFocusChangedEvent = event;
        isGame = true;
    }

    public void searchNoResult() {

        if (isGame) {
            mTitleView.setText(Html.fromHtml("<html><head><title>"
                    + getResources().getString(R.string.no_search_result_gamecenter)
                    + "</title></head><body><p>"
                    + getResources().getString(R.string.try_hot_search)
                    + "</p></body></html>"));
        } else {
            mNoSearchTip.setVisibility(View.VISIBLE);
            mTitleView.setText(R.string.try_hot_search);
            LayoutParams textParams = new LayoutParams(LayoutParams.WRAP_CONTENT,
                    LayoutParams.WRAP_CONTENT, Gravity.TOP | Gravity.LEFT);
            textParams.setMargins(SkyConstants.Div(105), SkyConstants.Div(485), 0, 0);
            contentLayout.removeView(mTitleView);
            contentLayout.addView(mTitleView, textParams);
        }

        LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
        if (isGame)
            params.setMargins(SkyConstants.Div(25), SkyConstants.Div(200), 0, 0);
        else
            params.setMargins(SkyConstants.Div(70), SkyConstants.Div(420), 0, 0);
        mHotLayout.setLayoutParams(params);
    }


    public void searchHotResult() {
        if (isGame) {
            mTitleView.setText(R.string.hot_search);
            LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
            params.setMargins(SkyConstants.Div(25), SkyConstants.Div(125), 0, 0);
            mHotLayout.setLayoutParams(params);
        } else {
            mNoSearchTip.setVisibility(View.INVISIBLE);
            mTitleView.setText(R.string.hot_search);
            LayoutParams textParams = new LayoutParams(LayoutParams.WRAP_CONTENT,
                    LayoutParams.WRAP_CONTENT, Gravity.TOP | Gravity.LEFT);
            textParams.setMargins(SkyConstants.Div(105), SkyConstants.Div(90), 0, 0);
            contentLayout.removeView(mTitleView);
            contentLayout.addView(mTitleView, textParams);
            LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
            params.setMargins(SkyConstants.Div(70), SkyConstants.Div(5), 0, 0);
            mHotLayout.setLayoutParams(params);
        }
    }

    public SearchController getController() {
        return mController;
    }

    public IAudio getAudioInterface() {
        return mController;
    }

    @Override
    public void setLoadingVisible(boolean visible) {
        if (mShowLoadingListener != null) {
            mShowLoadingListener.showLoading(visible);
        } else {
            super.setLoadingVisible(visible);
        }
    }

    private void addTitle() {
        if (isGame) {
            mTitleView = new TextView(mContext);
            LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT,
                    LayoutParams.WRAP_CONTENT, Gravity.TOP | Gravity.LEFT);
            params.setMargins(SkyConstants.Div(25), SkyConstants.Div(100), 0, 0);
            mTitleView.setText(R.string.hot_search);
            mTitleView.setTextColor(Color.WHITE);
            mTitleView.setTextSize(SkyConstants.Dpi(46));
            contentLayout.addView(mTitleView, params);
        } else {
            mTitleView = new TextView(mContext);
            LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT,
                    LayoutParams.WRAP_CONTENT, Gravity.TOP | Gravity.LEFT);
            params.setMargins(SkyConstants.Div(105), SkyConstants.Div(295), 0, 0);
            mTitleView.setText(R.string.hot_search);
            mTitleView.setTextColor(Color.WHITE);
            mTitleView.setTextSize(SkyConstants.Dpi(35));
            contentLayout.addView(mTitleView, params);

            mNoSearchTip = new TextView(mContext);
            params = new LayoutParams(LayoutParams.WRAP_CONTENT,
                    LayoutParams.WRAP_CONTENT, Gravity.CENTER_HORIZONTAL);
            params.topMargin = SkyConstants.Div(225);
            mNoSearchTip.setText(R.string.no_search_result_appstore);
            mNoSearchTip.setTextColor(Color.WHITE);
            mNoSearchTip.setTextSize(SkyConstants.Dpi(40));
            mNoSearchTip.setVisibility(View.INVISIBLE);
            contentLayout.addView(mNoSearchTip, params);
        }
    }

    private void addHotLayout() {
        List<BlockData> blockDataList = createHotBlockDatas();
        mHotLayout = new SkyCustomLayout(mContext, CoocaaApplication.mDiv);
        CustomLayoutParams netAppParams = new CustomLayoutParams();
        if (isGame) {
            netAppParams.mRow = 8;
            netAppParams.mCol = 1;
            netAppParams.mBlockWidth = SkyConstants.Div(975);
            netAppParams.mBlockHeight = SkyConstants.Div(142);
            netAppParams.mHorizontalSpace = SkyConstants.Div(0);
            netAppParams.mVerticalSpace = SkyConstants.Div(-50);
            netAppParams.mMarginTop = SkyConstants.Div(70);
            // netAppParams.mMarginLeft = SkyConstants.Div(20);
            mHotLayout.setCustomLayoutParams(netAppParams);
            mHotLayout.setBlockDatas(blockDataList);
            SkySearchHotAdapter mAdapter = new SkySearchHotAdapter(mContext);
            mAdapter.setFocusChangedEvent(mFocusChangedEvent);
            mAdapter.setKeyListener(mController);
            mHotLayout.setAdapter(mAdapter);
            //mAdapter.refreshUI(getList());
            LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
            params.setMargins(SkyConstants.Div(25), SkyConstants.Div(125), 0, 0);
            contentLayout.addView(mHotLayout, params);
        } else {
            netAppParams.mRow = 4;
            netAppParams.mCol = 2;
            netAppParams.mBlockWidth = SkyConstants.Div(490);
            netAppParams.mBlockHeight = SkyConstants.Div(245);
            netAppParams.mHorizontalSpace = SkyConstants.Div(0);
            netAppParams.mVerticalSpace = SkyConstants.Div(-36);
            netAppParams.mMarginTop = SkyConstants.Div(144);
            mHotLayout.setCustomLayoutParams(netAppParams);
            mHotLayout.setBlockDatas(blockDataList);
            SkyListAdapter mAdapter = new SkyListAdapter(mContext, ListType.SEARCH_HOT_LIST, 0, CoocaaApplication.mDiv);
            mAdapter.setFocusChangedEvent(mFocusChangedEvent);
            mAdapter.setKeyListener(mController);
            mHotLayout.setAdapter(mAdapter);
            LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
            params.setMargins(SkyConstants.Div(70), SkyConstants.Div(200), 0, 0);
            contentLayout.addView(mHotLayout, params);
        }

        mController.setHotLayout(mHotLayout);
    }

    public void addTips() {
        tipText = UICommon.newTextView(mContext, UICommon.tipTextSize, R.color.tipColor);
        tipText.setCompoundDrawablePadding(SkyConstants.Div(18));
        tipText.setCompoundDrawablesWithIntrinsicBounds(SkinManager.getResDrawable(R.drawable.cart_icon), null, null, null);
        this.addView(tipText, new LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT, Gravity.CENTER));
        tipText.setVisibility(View.INVISIBLE);
    }

    protected void initFlipper() {
        int row = 4, column = 2;
        listFlipper = new ListFlipper(mContext, Direction.VERTICAL);
        mController = new SearchController(mContext, listFlipper, row, column);
        mController.setGame(isGame);
        AppStoreSearchController.getAppStoreSearchController().setSearchDataListener(mController);
        mController.setContentLayout(this);
        List<BlockData> blockDataList = createListBlockData(row, column);
        for (int i = 0; i < ListFlipper.VIEW_PAGE_COUNT; i++) {
            SkyCustomLayout page = new SkyCustomLayout(mContext, CoocaaApplication.mDiv);
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                    LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
            page.setLayoutParams(params);
            CustomLayoutParams netAppParams = new CustomLayoutParams();
            netAppParams.mRow = row;
            netAppParams.mCol = column;
            netAppParams.mBlockWidth = SkyConstants.Div(490);
            netAppParams.mBlockHeight = SkyConstants.Div(245);
            netAppParams.mHorizontalSpace = SkyConstants.Div(0);
            netAppParams.mVerticalSpace = SkyConstants.Div(-36);

            if(isGame)
                netAppParams.mMarginTop = SkyConstants.Div(120);
            else
                netAppParams.mMarginTop = SkyConstants.Div(84);
            //netAppParams.mMarginLeft = SkyConstants.Div(20);
            page.setCustomLayoutParams(netAppParams);
            page.setBlockDatas(blockDataList);
            SkyListAdapter mAdapter = new SkyListAdapter(mContext, listType, i, CoocaaApplication.mDiv);
            mAdapter.setFocusChangedEvent(mFocusChangedEvent);
            mAdapter.setKeyListener(mController);
            mAdapter.setOnItemClickListener(this);
            mAdapter.setMultiSelectListener(mController);
            page.setAdapter(mAdapter);
            listFlipper.addView(page);
        }

        if(isGame)
            contentLayout.addView(listFlipper, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT, Gravity.CENTER));
        else {
            LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
            params.setMargins(SkyConstants.Div(70), SkyConstants.Div(65), 0, 0);
            contentLayout.addView(listFlipper, params);
        }
    }

    public static List<SkyListAppData> getList() {
        List<SkyListAppData> datas = new ArrayList<SkyListAppData>();
        for (int i = 0; i < 8; i++) {
            SkyListAppData data = new SkyListAppData("aa", "应用", "http://j.zdmimg.com/201311/11/528074a83f567.jpg_v1.jpg", "不错", "com.tianci.appstore");
            datas.add(data);
        }
        return datas;
    }

    @Override
    public void onPageChange(int currentPage, int totalPage) {
        LogUtils.d("refresh search page is current = " + currentPage + " total = " + totalPage);
        pageNumberText.setText(String.format("%d/%d%s",
                currentPage, totalPage, mContext.getResources().getString(R.string.page)));
    }

    protected List<BlockData> createListBlockData(int row, int column) {
        List<BlockData> datas = new ArrayList<BlockData>();
        for (int i = 0; i < row * column; i++) {
            BlockData data = new BlockData();
            data.mColNum = 1;
            data.mRowNum = 1;
            data.mBlock = new int[]{i};
            boolean upTurn = false;
            boolean downTurn = false;
            if (i < column)
                upTurn = true;
            if (i > 5)
                downTurn = true;
            data.mTurnPage = new boolean[]{false, upTurn, false, downTurn};
            datas.add(data);
        }
        return datas;
    }

    private List<BlockData> createHotBlockDatas() {
        List<BlockData> datas = new ArrayList<BlockData>();
        int count = 8;
//        if (!isGame)
//            count = 6;
        for (int i = 0; i < count; i++) {
            BlockData data = new BlockData();
            data.mColNum = 1;
            data.mRowNum = 1;
            data.mBlock = new int[]{i};
            data.mTurnPage = new boolean[]{false, false, false, false};
            datas.add(data);
        }
        return datas;
    }

    private void initPageNoView() {
        pageNumberText = UICommon.newTextView(mContext, SkyConstants.Dpi(33), R.color.pageNumberColor);
        LayoutParams pageParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.RIGHT | Gravity.BOTTOM);
        if(isGame) {
            pageParams.bottomMargin = SkyConstants.Div(48);
            pageParams.rightMargin = SkyConstants.Div(48);
        } else {
            pageParams.bottomMargin = SkyConstants.Div(28);
            pageParams.rightMargin = SkyConstants.Div(85);
        }



        contentLayout.addView(pageNumberText, pageParams);
        mController.setPageChangeListener(this);
    }

    protected void resetFocus() {
        SkyCustomLayout child = (SkyCustomLayout) listFlipper.getChildAt(listFlipper.getDisplayedChild());
        if (child != null)
            child.setFocusPos(0, true);
    }

    protected void setTipVisible(boolean visible, String tipStr) {
        if (visible) {
            contentLayout.setVisibility(View.INVISIBLE);
            tipText.setText(tipStr);
            tipText.setVisibility(View.VISIBLE);
        } else {
            tipText.setVisibility(View.INVISIBLE);
            contentLayout.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onBlockClicked(View v, SkyListAppData data, int index) {
        // TODO Auto-generated method stub
    }

    @Override
    public void destroyController() {
        AppStoreSearchController.getAppStoreSearchController().destroy();
    }

    @Override
    public void resume() {
        // TODO Auto-generated method stub

    }
}
